#include "SrAudioThread.h"

SrAudioThread::SrAudioThread()
    :m_audio_decode(NULL)
    ,m_audio_resample(NULL)
    ,m_audio_a_play(NULL)
    ,_isExit(false)
{

}
SrAudioThread::~SrAudioThread()
{

}

bool SrAudioThread::Open(AVCodecParameters *codecpar, int sampleRate, int channels)
{
    if (!codecpar)
    {
        return false;
    }
    audio_cur_pts = 0;

    if (!m_audio_decode)
    {
        m_audio_decode = new SrDecode();
    }

    if (!m_audio_resample)
    {
        m_audio_resample = new SrResample();
    }

    if (!m_audio_a_play)
    {
        m_audio_a_play  = SrAudioPlay::Get();
    }


    bool re = true;
    if (!m_audio_resample->Open(codecpar, false))
    {
        re = false;
        qDebug("XResample open failed!\n");
    }

    m_audio_a_play->_sampleRate = sampleRate;
    m_audio_a_play->_channels = channels;
    if (!m_audio_a_play->Open())
    {
        re = false;
        qDebug("SrAudioPlay open failed!\n");
    }

    if(!m_audio_decode->Open(codecpar, true))
    {
        re = false;
        qDebug("audio SrDecode open failed!\n");
    }

    return re;
}


void SrAudioThread::Close()
{
    // 等待线程退出
    _isExit = true;
    QThread::wait();

    Clear();

    m_audio_decode->Close();


    delete m_audio_decode;
    m_audio_decode = NULL;


    if (m_audio_resample)
    {
        m_audio_resample->Close();

        delete m_audio_resample;
        m_audio_resample = NULL;

    }
    if (m_audio_a_play)
    {
        m_audio_a_play->Close();
        m_audio_a_play = NULL;
    }

}
void SrAudioThread::Clear()
{
    _mux.lock();
    while (!_packs.empty())
    {
        AVPacket *pkt = _packs.front();
        av_packet_free(&pkt);
        _packs.pop_front();
    }
    _mux.unlock();
}
void SrAudioThread::Push(AVPacket *pkt)
{
    if(NULL == pkt)
    {
        return;
    }

    while(false == _isExit)
    {
        _mux.lock();

        if (_packs.size() < _maxList)
        {
            _packs.push_back(pkt);
            _mux.unlock();
            break;
        }
        _mux.unlock();
        msleep(1);
    }

}

void SrAudioThread::run()
{
    unsigned char *pcm = new unsigned char[1024 * 1024 * 10];
    while (!_isExit)
    {
        _mux.lock();

        // 没有数据
        if (_packs.empty() || !m_audio_decode || !m_audio_resample || !m_audio_resample)
        {
            _mux.unlock();
            msleep(1);
            continue;
        }

        AVPacket *pkt = _packs.front();
        _packs.pop_front();


        bool re = m_audio_decode->Send(pkt);
        if (!re)
        {
            _mux.unlock();  //TODO
            msleep(1);
            continue;
        }
        // 一次Send可能有多次recv
        while (!_isExit)
        {
            AVFrame *frame = m_audio_decode->Recv();
            if (!frame) break;

            //当前音频的pts
            audio_cur_pts = frame->pts - m_audio_a_play->GetNoPlayMs();

            // 重采样
            int size = m_audio_resample->Resample(frame, pcm);
            // 播放音频
            while (size > 0)
            {
                if (m_audio_a_play->GetFree() >= size)
                {
                    m_audio_a_play->Write(pcm, size);
                    break;
                }
                msleep(1);
            }
        }
        _mux.unlock();  //TODO
    }

    delete [] pcm;
}

